package yxy.game.pm2.command.conquest;

import yxy.cherry.battle.module.record.result.BattleResult;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.pubsub.ConquestPublisher;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ConquestMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.battle.BattleModule;
import yxy.game.pm2.module.conquest.ConquestHandle;
import yxy.game.pm2.module.conquest.ConquestModule;

import java.util.Date;

/**
 * 征战 战斗结算
 */
@MessageType(proto = Command.ConquestEnemyBattleClear, classtype = ConquestMessage.ConquestEnemyBattleClearRequest.class)
public class CMD_ConquestEnemyBattleClear extends Command<ConquestMessage.ConquestEnemyBattleClearRequest> {

    @Override
    public void execute(MessageSource messageSource, ConquestMessage.ConquestEnemyBattleClearRequest request) {
        Date date1 = new Date();

        ConquestMessage.ConquestEnemyBattleClearResponse.Builder response = ConquestMessage.ConquestEnemyBattleClearResponse.newBuilder();
        try {

            String playerCode = messageSource.getTag();
            String battleCode = request.getBattleCode();

            BattleModule battleModule = BattleModule.get(playerCode, battleCode);
            if (battleModule.isSettled()) {
                send(messageSource, response, Result.BattleHasSettled);// 战斗已结算
                return;
            }
            if (!battleModule.isFinished()) {
                send(messageSource, response, Result.BattleIsNotFinished);// 战斗尚未结束
                return;
            }

            ConquestHandle handle = new ConquestHandle();
            if (handle.isBattling(battleModule)) {
                if (!battleModule.isSkip()) {
                    send(messageSource, response, Result.BattleIsNotFinished);// 战斗尚未结束
                    return;
                }
            }

            response.setInfo(new BattleHandle().battleClear(battleModule));
            battleModule.setSettled(true);

            BattleResult battleResult = battleModule.getBattleResult();
            if (!battleResult.equals(BattleResult.LEFT_WIN)) {
                battleModule.set(ConquestHandle.battleExpireDay);
                send(messageSource, response, Result.Success);
                return;
            }

            battleModule.getSettlement().settle();
            battleModule.set(ConquestHandle.battleExpireDay);

            ConquestModule conquestModule = ConquestModule.get(playerCode);
            int mapId = conquestModule.getMapId();
            PlayerBean player = PlayerBean.get(playerCode);
            response.setMapId(mapId);
            response.setLevel(player.getLevel());
            response.setExp(Currency.number(playerCode, CurrencyType.CT_主公经验, 0,false));
            response.addAllRewards(new CurrencyHandle().packItems(battleModule.getSettlement().reward()));

            send(messageSource, response, Result.Success);

            ConquestPublisher.getInstance().publish(playerCode, mapId);

        } catch (Exception e) {
            e.printStackTrace();
        }

        Date date2 = new Date();
        System.out.println("CMD_ConquestEnemyBattleClear:" + (date2.getTime() - date1.getTime()));
    }

    private void send(MessageSource messageSource, ConquestMessage.ConquestEnemyBattleClearResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}